/*
 * Copyright (C) 2011 xjuraj
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.document.preprocessing.actions;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.extract.text.Flash2Text;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class FlashTextExtract implements Serializable, PreprocessAction{
    
    private transient Logger log = Logger.getLogger(FlashTextExtract.class);
    private transient PropertiesUtils pu = new PropertiesUtils(FlashTextExtract.class);

    @Override
    public WebSearchResponse preprocess(WebSearchResponse response) {
        if(response.getFile() == null){
            return response;
        }
        File fileResult = null;
        try {
            Flash2Text flash2Text = new Flash2Text();
            String extractText = flash2Text.extractText(response.getFile());

            fileResult = sk.lieskove.jianghongtiao.common.utils.FileUtils.getTempFile("Flash", "");
            FileUtils.writeStringToFile(fileResult, extractText, "UTF-8");
            response.setFile(fileResult);
        } catch (IOException ex) {
            log.error("Cannot write Flash transformation to file: "+fileResult.getAbsolutePath());
        }
        return response;
    }
}
